<?php require_once('../../Connections/cotiza.php'); ?>
<?php require_once('../../include/defaults.php'); ?>
<?php require_once("../../include/language.php"); ?>
<?php require_once("../lib/functions.php"); ?>
<?php
noCache();
?>
<?php
//session_start();
mysql_select_db($database_cotiza, $cotiza);

$acc     = $_GET['acc'];
$id_mach = $_GET['id_mach'];
$id_mch  = $_GET['id_mch'];

if($acc == "save")
{
	$cod    = $_POST['cod'];
	$desc   = $_POST['desc'];
	$proc   = $_POST['proceso'];
	$vel    = $_POST['velocidad'];
	$tarifa = $_POST['tarifa'];
	
	$query_insert = "INSERT INTO mach_mstr (mach_id, mach_desc, mach_proc, mach_velocidad) 
	                                 VALUES('$cod', '$desc', $proc, '$vel')
				    ON DUPLICATE KEY UPDATE mach_desc = '$desc', mach_proc = $proc, mach_velocidad = '$vel'";
	mysql_query($query_insert, $cotiza) or die(mysql_error());
	
	//costos
	$i=0;
	foreach($tarifa as $tr)
	{
		$i++;
		$query_costos = "INSERT INTO costm_det (costm_mch_id, costm_cost_id, costm_costo)
	                                VALUES ('$cod',$i,'$tr')
				   ON DUPLICATE KEY UPDATE costm_costo = '$tr'";
	mysql_query($query_costos,$cotiza);
	}
	
	//echo $query_insert;
	//header("Location: mntomaquinas.php");
}

if($acc == "edit" and $id_mach != "")
{	
	//$id_mach = $cod_ext;
	$query     = "SELECT * FROM mach_mstr where mach_id = '$id_mach'";
	//echo $query;
	$registros = mysql_query($query, $cotiza) or die(mysql_error());
	$row       = mysql_fetch_assoc($registros);
	$totalRows = mysql_num_rows($registros);
	$edit      = 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>...::: MNTO Maquinas :::...</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/funciones.js"></script>
</head>

<body>
<strong>Maquina</strong>
<form id="form1" name="form1" method="post" action="mntomaquinas.php?acc=save">
<table border="0">
  <tr>
    <td width="120">Cod. Maquina</td>
    <td width="4">:</td>
    <td width="100"><input name="cod" type="text" class="default <?php if($id_mach != "") echo "borde_trasparente"; ?>" id="cod" size="20" autocomplete="off" <?php if($id_mach != "") echo "readonly=\"readonly\" value=\"$id_mach\""; else echo " onkeypress=\"check_key(event,'mntomaquinas.php?acc=edit&id_mach=' + this.value)\" value=\"$id_mch\""; ?>/></td>
    <td width="97"><?php if($id_mach == "") { ?><a href="javascript:abrir('../browser/maquinas.php','cod',370,450)"><img src="../img/find.png" width="16" height="16" border="0" /></a><?php } ?></td>
  </tr>
  <tr>
    <td>Descripci&oacute;n</td>
    <td>:</td>
    <td colspan="2"><input name="desc" type="text" class="default" id="desc" size="40" <?php if($id_mach == "") echo "style=\"visibility:hidden\""; ?> value="<?php echo $row['mach_desc']; ?>" /></td>
    
  </tr>
</table>
<br />
<div id="detail-ext" style=" <?php if($id_mach == "") echo "visibility:hidden"; ?>">
<strong>Datos T&eacute;cnicos</strong><br />
<table border="0">
  <tr>
    <td width="120">Proceso</td>
    <td width="4">:</td>
    <td>
    <?php 
		$query     = "SELECT * FROM proc_mstr";
		$rslt_proc = mysql_query($query,$cotiza);
		$row_proc  = mysql_fetch_assoc($rslt_proc);
	?>
    <select name="proceso" class="default" id="proceso">
    <?php 
		do{
	?>
      <option value="<?php echo $row_proc['proc_id']?>" <?php if($row['mach_proc'] == $row_proc['proc_id']) echo "selected=\"selected\"";?>><?php echo $row_proc['proc_desc']?></option>
    <?php 
		}while($row_proc  = mysql_fetch_assoc($rslt_proc))
    ?> 
    </select></td>
    </tr>
  <tr>
    <td>Velocidad</td>
    <td>:</td>
    <td><input name="velocidad" type="text" class="default" id="velocidad" onkeypress="return valida_numeros(event)" size="6" value="<?php echo $row['mach_velocidad']; ?>" autocomplete="off"/></td>
  </tr>
  <?php 
  //Costos
  $query     = "SELECT * FROM cost_mstr";
  $rslt_cost = mysql_query($query,$cotiza) or die(mysql_error());
  $row_cost  = mysql_fetch_assoc($rslt_cost);
  do{
  ?>
  <tr>
    <td><?php echo $row_cost['cost_desc'];?></td>
    <td>:</td>
    <td>
    <?php 
		$cost_id    = $row_cost['cost_id'];
		$query      = "SELECT * FROM costm_det WHERE costm_mch_id = '$id_mach' AND costm_cost_id = $cost_id";
		$rslt_costd = mysql_query($query,$cotiza);
		$row_costd  = mysql_fetch_assoc($rslt_costd);
		$costo      = $row_costd['costm_costo'];
	?>
    <input name="tarifa[<?php echo $cost_id;?>]" type="text" onkeypress="return valida_numeros(event)" class="default" id="tarifa[<?php echo $cost_id;?>]" size="6" value="<?php echo $costo; ?>" /></td>
  </tr>
  <?php 
  }while($row_cost  = mysql_fetch_assoc($rslt_cost));
  ?>
</table>
</div>
<script>
try{
	var foc = '<?php echo $id_mach;?>';
	
	if(foc != "")
		document.getElementById('desc').focus();
	else
		document.getElementById('cod').focus();
}
catch(e)
{
}
</script>
<br />
</form>
<br />
<table width="479" border="0">
  <tr>
    <td align="left" class="descripcion_alpie" ><?php if($acc == "edit" and $totalRows == 0)echo $text_newreg; if($acc == "edit" and $totalRows == 1)echo $text_editreg; ?></td>
    <td width="46" align="right"><input name="button2" type="button" onclick="window.open('mntomaquinas.php?id_mch=<?php echo $id_mach; ?>','_self');" class="default" id="button2" value="Volver" <?php if($id_mach == "") echo "disabled=\"disabled\""; ?> /></td>
    <td width="56" align="right"><input name="Submit" type="button" class="default" value="Guardar" onclick="document.forms.form1.submit()" <?php if($id_mach == "") echo "disabled=\"disabled\""; ?> /></td>
    </tr>
</table>

</body>
</html>
<?php 
mysql_close($cotiza);
?>